const { login } = require("../controller/user");
const { set } = require("../db/redis");
const { SuccessModel, ErrorModel } = require("../model/res");

const handleUserRouter = (req, res) => {
  const method = req.method;

  if (method === "POST" && req.path === "/api/user/login") {
    let { username, password } = req.body;
    return login(username, password).then((data) => {
      if (data.username) {
        req.session.username = data.username;
        req.session.realname = data.realname;
        set(req.sessionId, req.session);
        console.log(req.session, "req.session", req.sessionId);
        // if (req.needSetCookie) {
        res.setHeader("Set-Cookie", [`sessionId=${req.sessionId}`]);
        // res[Headers]["Set-Cookie"] = `sessionId = ${req.sessionId}`;
        // }
        return new SuccessModel(req.session);
      }
      return new ErrorModel("fail");
    });
    // SuccessModel,ErrorModel
  }
};
module.exports = handleUserRouter;
